package cn.com.shbs.Controller;

import cn.com.shbs.Config.CommonUtil;
import cn.com.shbs.Config.PasswordHelper;
import cn.com.shbs.Dao.EmployeeInfoDao.EmployeeInfoDao;
import cn.com.shbs.Dao.LoginUserDao.LoginUserDao;
import cn.com.shbs.Service.EmployeeInfoService.EmployeeInfoService;
import cn.com.shbs.Service.code.SerialNumberGeneratorService;
import cn.com.shbs.model.EmployeeInformation;
import cn.com.shbs.model.LoginUser;
import cn.com.shbs.Config.CommonUtil;
import cn.com.shbs.model.UserInfo.UserInfo;
import net.sf.json.JSONObject;
import org.apache.catalina.servlet4preview.http.HttpServletRequest;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;

@RestController
@RequestMapping("API/")
public class EmployeeInfoController {
    static Logger logger = LoggerFactory.getLogger(EmployeeInfoController.class);
    @Autowired
    LoginUserDao loginUserDao;
    @Autowired
    EmployeeInfoService employeeInfoService;
    @Autowired
    EmployeeInfoDao employeeInfoDao;
    @Autowired
    SerialNumberGeneratorService serialNumberGeneratorService;
    @RequestMapping("/addemployeeinfo")
    public Object addemployeeinfo(HttpServletRequest request, LoginUser userInfo, EmployeeInformation employeeInformation){
        String message="error";
        JSONObject result=new JSONObject();
        try{
            String user_code= CommonUtil.PREFIX_SYSUSER+serialNumberGeneratorService.getCodeGeneratorstr(CommonUtil.STEPNUM,CommonUtil.SYSUSER_TYPE);
            employeeInformation.setUser_code(user_code);
            //判断当前用户是否存在
            LoginUser us=null;
            //根据登录名查询是否唯一
            us =  loginUserDao.findByloginname(userInfo.getLogin_name());
            if (us!=null){
                message="nameexit";
                result.put("message",message);
                return result;
            }

            //不存在设置用户参数
            PasswordHelper helper=new PasswordHelper();
            helper.encryptPassword(userInfo);
            byte State = 0;
            userInfo.setUser_code(user_code);
            employeeInformation.setUser_code(user_code);
            employeeInfoService.addemployeeinfo(userInfo,employeeInformation);
            message = "success";
        }catch (Exception e){
            logger.error("EmployeeInfoController.addstoreinfo()",e);
            e.printStackTrace();
        }
        result.put("message",message);
        return result.toString();
    }

    @RequestMapping("/queryemployeeinfo")
    public Object queryemployeeinfo(HttpServletRequest request,String user_code){
        String message="error";
        JSONObject result=new JSONObject();
        List<HashMap> clist = new ArrayList<HashMap>();
        try{
            HashMap map = new HashMap();
            map.put("user_code",user_code);
            clist = employeeInfoDao.queryemployeeinfo(map);
            message = "success";
            result.put("clist",clist);
        }catch (Exception e){
            logger.error("EmployeeInfoController.querystoreinfo()",e);
            e.printStackTrace();
        }
        result.put("message",message);
        return result.toString();
    }

    @RequestMapping("/editemployeeinfo")
    public Object editemployeeinfo(HttpServletRequest request,String user_code,EmployeeInformation employeeInformation){
        String message="error";
        JSONObject result=new JSONObject();
        try{
            employeeInformation.setUser_code(user_code);
            employeeInfoService.editemployeeinfo(employeeInformation);
            message = "success";
        }catch (Exception e){
            logger.error("EmployeeInfoController.editemployeeinfo()",e);
            e.printStackTrace();
        }
        result.put("message",message);
        return result.toString();
    }


}
